﻿using B.S.BasicData.Domain;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infrastructure.Interfance;
using B.S.BasicData.Read.Apilication.Common.Role;
using MediatR;

namespace B.S.BasicData.Read.Apilication.Handler.Role
{
    public class GetRoleHandler : IRequestHandler<GetRoleCommon, ApiPaimg<RoleModel>>
    {
        private readonly IBaseRepository<RoleModel> roleRepo;

        public GetRoleHandler(IBaseRepository<RoleModel> roleRepo)
        {
            this.roleRepo = roleRepo;
        }
        public Task<ApiPaimg<RoleModel>> Handle(GetRoleCommon request, CancellationToken cancellationToken)
        {
            ApiPaimg<RoleModel> res = new ApiPaimg<RoleModel>();

            var role = roleRepo.GetAll().Where(x => x.IsDeleted == false);


            res.ToTaCount = role.Count();
            res.ToTaPage = (int)Math.Ceiling(res.ToTaCount * 1.0 / request.PageSize);
            res.PageData = role.OrderByDescending(x => x.RoleId).Skip((request.pageindex - 1) * request.PageSize).Take(request.PageSize).ToList();
            return Task.FromResult(res);
           

        }
    }
}
